﻿using FlickrNet;
using Infrastructure.BLL;
using MYWebsite;
using System;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Net;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWebsite
{
    public partial class Gallery : System.Web.UI.Page
    {

        public static Flickr flickerRef = new Flickr(ConfigurationManager.AppSettings["apiKey"], ConfigurationManager.AppSettings["shardSecret"]);

        #region handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            Flickr.CacheDisabled = true;
            var photoSets = FlickrBLL.GetPhotoSets(flickerRef);
            const string albumQueryStringKey = "AlbumId";
            var albumId = string.Empty;

            if (!IsPostBack)
            {
                if (Context.Request.QueryString.Count != 0)
                {
                    albumId = Convert.ToString(Context.Request.QueryString[albumQueryStringKey]);
                    PhotosetPhotoCollection photoesInCurrentSet = FlickrBLL.GetPhotsInaSet(albumId, flickerRef);
                    if (photoesInCurrentSet != null && photoesInCurrentSet.Count > 0)
                    {

                        RenderImagesInaSet(photoesInCurrentSet);


                    }
                }

                else
                {
                    if (photoSets != null)
                    {
                        var elm = "<ul id='imagesets'>";
                        galleryimages.Text = elm;
                        var imagesHtml = "<li class='multiple-borders cursorHand albumClick' albumId = '{0}' imgCount = '{4}'><img id = '{0}' src='{1}' class='hastooltip' /><div class='hide'><p>Album Title : {2}</p><span>Album Description : {3}</span></div></li>";
                        foreach (var set in photoSets)
                        {
                            galleryimages.Text += string.Format(imagesHtml, set.PhotosetId, set.PhotosetSmallUrl, set.Title, set.Description, set.NumberOfPhotos);
                        }
                        galleryimages.Text += "</ul>";
                    }
                    else
                    {
                        //show message No Images added yet 
                        galleryimages.Text = "No Images added yet or You are playing with album name. Stop it ! ";
                    }
                }
            }
        }

        #endregion handlers


        #region helperMethods

        void GetImagesForAlbum(object sender, ImageClickEventArgs e)
        {
            // do something
            var albumId = ((System.Web.UI.Control)(sender)).ID;
            if (!string.IsNullOrEmpty(albumId))
            {

                var allPhotosInSet = FlickrBLL.GetPhotsInaSet(albumId, flickerRef);

            }

        }

        void RenderImagesInaSet(PhotosetPhotoCollection photos)
        {
            galleryimages.Text = string.Empty;
            var elm = "<ul id='imagesets'>";
            galleryimages.Text = elm;
            var imagesHtml = "<li class='multiple-borders cursorHand' albumId = '{0}'> <span></span> <img id = '{0}' src='{1}' class='hastooltip'/><div class='hide'><p>Album Title : {2}</p><span>Album Description : {3}</span></div></li>";
            foreach (var image in photos)
            {
                galleryimages.Text += string.Format(imagesHtml, image.PhotoId, image.SmallUrl, image.Title, image.Description);
            }
            galleryimages.Text += "</ul>";
        }

        protected void DownloadAllImagesInaPhotoSet(string photosetId)
        {
            var photoSets = FlickrBLL.GetPhotoSets(flickerRef);
            foreach (var item in photoSets)
            {
                if (item.PhotosetId == photosetId)
                {
                    var albumId = item.PhotosetId;
                    if (!string.IsNullOrEmpty(albumId))
                    {
                        var allPhotosInSet = FlickrBLL.GetPhotsInaSet(albumId, flickerRef);

                        foreach (var item1 in allPhotosInSet)
                        {
                            string exts = Path.GetExtension(item1.LargeUrl);
                            string strRealname = Path.GetFileName(item1.Title);
                            WebClient webClient = new WebClient();
                            webClient.DownloadFile(item1.LargeUrl, Server.MapPath("~/Downloads/") + strRealname + exts);

                        }
                    }

                }
            }
        }

        #endregion helperMethods
    }
}